<?php

require_once 'common/conn.php';
$result = array();

if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
}
if (isset($_GET['type'])) {
    $type = $_GET['type'];
} else {
    $type = "";
}
if (isset($_GET['flashcode'])) {
    $flashcode = $_GET['flashcode'];
} else {
    $flashcode = "";
}
if (isset($_GET['amount'])) {
    $amount = $_GET['amount'];
}

if (isset($usercode) && isset($amount)) {
    $usercode = trim($usercode);
    $sql = "INSERT INTO t_gold_record(usercode,type,flashcode,amount,createTime) VALUES('" . $usercode . "','" . $type . "','" . $flashcode . "'," . $amount . ",now())";
    $db = new DBC();

    if ($db->execute($sql)) {
        $result['errorNo'] = 0;
        $sql = "SELECT usercode,SUM(amount) AS amount FROM t_gold_record WHERE usercode = '" . $usercode . "'";
        $gold = $db->fetchData($sql);
        if ($gold != false && count($gold) > 0) {
            $amount = $gold[0]['amount'];        
            $sql = "UPDATE t_device SET gold = " . $amount . " WHERE devicecode = '" . $usercode . "'";
            $db->execute($sql);
        }
    } else {
        $result['errorNo'] = 1;
    }
} else {
    $result['errorNo'] = 1;
}

echo json_encode($result);
?>
